package com.tingyuge.thinking.spring.configuration.metadata;

import com.tingyuge.thinking.spring.ioc.overview.domain.User;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.EncodedResource;

/**
 * {@link PropertiesBeanDefinitionReader}  示例
 *
 * @author xiaodao
 * @version 1.0
 * @date 2021/7/13
 */
public class PropertiesBeanDefinitionReaderDemo {

    public static void main(String[] args) {
        //1.创建IOC 底层容器
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        //2.创建面向 Properties资源的 PropertiesBeanDefinitionReader 示例
        PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(beanFactory);
        //Properties 资源加载默认是ISO-8859-1,实际存储是UTF-8
        ResourceLoader resourceLoader = new DefaultResourceLoader();
        Resource resource = resourceLoader.getResource("META-INF/user-bean-definitions.properties");
        EncodedResource encodedResource = new EncodedResource(resource, "UTF-8");

        int beanDefinitions = reader.loadBeanDefinitions(encodedResource);
        System.out.println("记载着bean的数量: " + beanDefinitions);
        //通过依赖查找,查找bean
        User bean = beanFactory.getBean("user",User.class);
        System.out.println(bean);
    }
}
